package com.jweb.framework.ws.channel;

import java.io.IOException;

import javax.xml.transform.Source;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;
import org.springframework.xml.validation.XmlValidator;
import org.xml.sax.SAXParseException;

/**
 * 修改人： Thomas
 * 修改内容：新增 
 * 类说明：xsd验证
 */
public class SchemaValidator {
	
	private static final Logger logger = LoggerFactory.getLogger(SchemaValidator.class);
	
	private XmlValidator xmlValidator ;
	
	public SchemaValidator(XmlValidator xmlvalidator){
		this.xmlValidator = xmlvalidator ;
	}
	
	public boolean validate(Source source) throws IOException {
		
		SAXParseException saxes[] = xmlValidator.validate(source);
		if(!ObjectUtils.isEmpty(saxes)){
			return handleValidationErrors(saxes);
		}
		else {
			logger.debug("XML message is validated") ;
			return true ;
		}
		
	}
	
	protected boolean handleValidationErrors(SAXParseException saxes[]){
		for(int i = 0 ; i < saxes.length ; i++){
			logger.error((new StringBuilder("XML validation error: ")).append(saxes[i].getMessage()).toString());
		}
		
		return false ;
	}

}
